package com.gpy.hospital.cms.controller;

import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.pojo.Doctor;
import com.gpy.hospital.cms.pojo.Keshi;
import com.gpy.hospital.cms.vo.DoctorVo;
import com.gpy.hospital.common.ServerResponse;
import com.gpy.hospital.reception.service.IDoctorService;
import com.gpy.hospital.reception.service.IKeShiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Date 2020/2/28 20:03
 * @Desc
 **/
@Controller
public class DoctorController {

    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private IKeShiService keShiService;

    @GetMapping("/cms/doctor/page")
    public String doctorPage() {
        return "/cms/doctorList";
    }

    @GetMapping("/cms/doctor/list")
    @ResponseBody
    public ServerResponse userList(@RequestParam(value = "doctorName", defaultValue = "") String doctorName,
                                   @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        PageInfo<Doctor> pageInfo = doctorService.getByDoctorName(doctorName, pageNum, pageSize);
        return ServerResponse.createBySuccess("查询成功！", pageInfo.getList(), pageInfo.getTotal());
    }

    @GetMapping("/cms/doctor/detail")
    public String doctorDetail(@RequestParam("doctorId") Integer doctorId, Model model) {
        Doctor doctor = doctorService.getById(doctorId);
        String chuzhen = doctor.getChuzhen();
        String[] split = chuzhen.replace("[", "").replace("]", "").split(",");
        Map<String, String> map = new HashMap<>();
        map.put("a", split[0]);
        map.put("b", split[1]);
        map.put("c", split[2]);
        map.put("d", split[3]);
        map.put("e", split[4]);
        map.put("f", split[5]);
        map.put("g", split[6]);
        map.put("h", split[7]);
        map.put("i", split[8]);
        map.put("j", split[9]);
        map.put("k", split[10]);
        map.put("l", split[11]);
        map.put("m", split[12]);
        map.put("n", split[13]);
        model.addAttribute("chuzhen", map);
        model.addAttribute("doctor", doctor);
        List<Keshi> keshis = keShiService.getByStatus();
        model.addAttribute("keshis", keshis);
        return "/cms/doctorDetail";
    }

    @PostMapping("/cms/doctor/save")
    @ResponseBody
    public ServerResponse doctorSave(DoctorVo doctorVo) {
        return doctorService.saveDoctor(doctorVo);
    }

    @GetMapping("/cms/doctor/add")
    public String doctorAdd(Model model) {
        List<Keshi> keshis = keShiService.getByStatus();
        model.addAttribute("keshis", keshis);
        return "/cms/doctorAdd";
    }

    @GetMapping("/reception/doctor/detail")
    public String receptionDoctorDetail(@RequestParam("doctorId") Integer doctorId, Model model){
        Doctor doctor = doctorService.getById(doctorId);
        String chuzhen = doctor.getChuzhen();
        String[] split = chuzhen.replace("[", "").replace("]", "").split(",");
        Map<String, String> map = new HashMap<>();
        map.put("a", split[0].equals("1")?"出诊":"否");
        map.put("b", split[1].equals("1")?"出诊":"否");
        map.put("c", split[2].equals("1")?"出诊":"否");
        map.put("d", split[3].equals("1")?"出诊":"否");
        map.put("e", split[4].equals("1")?"出诊":"否");
        map.put("f", split[5].equals("1")?"出诊":"否");
        map.put("g", split[6].equals("1")?"出诊":"否");
        map.put("h", split[7].equals("1")?"出诊":"否");
        map.put("i", split[8].equals("1")?"出诊":"否");
        map.put("j", split[9].equals("1")?"出诊":"否");
        map.put("k", split[10].equals("1")?"出诊":"否");
        map.put("l", split[11].equals("1")?"出诊":"否");
        map.put("m", split[12].equals("1")?"出诊":"否");
        map.put("n", split[13].equals("1")?"出诊":"否");
        model.addAttribute("chuzhen", map);
        model.addAttribute("doctor", doctor);
        return "/reception/doctorDetail";
    }

    @GetMapping("/cms/doctor/delete")
    @ResponseBody
    public ServerResponse doctorDelete(@RequestParam("doctorId") Integer doctorId) {
        return doctorService.delete(doctorId);
    }



    @GetMapping("/cms/doctor/add/account")
    public String doctorAddAccountPage() {
        return "/cms/doctorAddAccount";
    }


}
